package top.codestudy.viewtest.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.ScrollView;

import androidx.annotation.Nullable;

public class CircleView extends View {
    private String TAG = getClass().getSimpleName();

    private Paint circlePaint;
    private RectF rectF = new RectF();

    public CircleView(Context context) {
        this(context, null);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();

    }

    private void init() {
        circlePaint = new Paint();
        circlePaint.setAntiAlias(true);
        circlePaint.setColor(Color.BLUE);
        circlePaint.setStrokeCap(Paint.Cap.ROUND);
        circlePaint.setStrokeWidth(40);
        circlePaint.setStyle(Paint.Style.FILL);


    }

    public boolean isFirst = true;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.parseColor("#77875623"));
        //canvas.drawCircle(getWidth()/2.0f,getHeight()/2.0f,getWidth()/2.0f,circlePaint);
        if (isFirst) {
            rectF.left = -getWidth() / 2.0f;
            rectF.top = 50;
            rectF.right = getWidth() / 2.0f - 50;
            rectF.bottom = getHeight() - 50;
            isFirst =false;
        }

        circlePaint.setColor(Color.BLUE);
        canvas.drawArc(rectF, 0, 90, true, circlePaint);
        circlePaint.setColor(Color.parseColor("#FF4040"));
        canvas.drawArc(rectF, 90, 60, true, circlePaint);

        circlePaint.setColor(Color.parseColor("#9932CC"));
        canvas.drawArc(rectF, 150, 210, true, circlePaint);
    }

    private long timeS = System.currentTimeMillis();

    float lastX;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int TouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                lastX = event.getRawX();
                Log.i(TAG, "onTouchEvent: down lastX==> " + lastX);
                break;
            case MotionEvent.ACTION_MOVE:
                if(Math.abs(event.getRawX() - lastX )  < TouchSlop){
                    break;
                }
                if(event.getRawX() - lastX > 0){
                    //++
                    rectF.left += (event.getRawX() - lastX) + 4;
                    rectF.top += 4;
                    rectF.right += (event.getRawX() - lastX) - 4;
                    Log.i(TAG, "onTouchEvent: move event.getRawX() - lastX==> "+ (event.getRawX() - lastX));
                    rectF.bottom -= 4;
                }else {
                    //--
                    rectF.top -= 4;
                    rectF.left += (event.getRawX() - lastX) - 4;
                    rectF.right += (event.getRawX() - lastX) + 4;
                    Log.i(TAG, "onTouchEvent: move event.getRawX() - lastX==> "+ (event.getRawX() - lastX));
                    rectF.bottom += 4;
                }

                lastX = event.getRawX();
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                Log.i(TAG, "onTouchEvent: up");
                break;
            case MotionEvent.ACTION_CANCEL:
                Log.i(TAG, "onTouchEvent: cancel");
                break;
        }
//        if (event.getAction() == MotionEvent.ACTION_MOVE && (timeS + 10000) < System.currentTimeMillis()) {
//            Log.i(TAG, "onInterceptTouchEvent: " + System.currentTimeMillis() + " ;; timeS = > " + timeS);
//            return false;
//        }
        return true;
    }

    ScrollView scrollView;
}
